//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    baanerList: [],
    infoList: [],
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 500,
    url: app.data.imgUrl,
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    infoParams: { // 信息列表的参数
      page: 1,
      numPerPage: 5
    },
    isTeamLogin: false, //管理员登录状态
    isfristload: false, //第一次加载
    isBottom: true, //底部是否有数据标志
    isTop: true, //顶部是否有数据标志
    isLoodingHide: true //加载动画
  },
  onLoad: function(option) {
    // 判断当前是否社团登陆
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    this.refirsh()
  },
  //点赞事件
  zanClick(e) {
    let item = e.currentTarget.dataset.item
    let index = e.currentTarget.dataset.index
    if (item.upStatus) {
      this.cancelUpNews(item.id, index)
    } else {
      this.upNews(item.id, index)
    }
  },
  //加上赞
  upNews(id, index) {
    wx.teamApi.news.upNews(id).then(res => {
      if (res.data.errcode === 0) {
        this.data.infoList[index].upStatus = 1
        this.data.infoList[index].upNum += 1
        this.setData({
          infoList: this.data.infoList
        })
      }
    })
  },
  //取消赞
  cancelUpNews(id, index) {
    wx.teamApi.news.cancelUpNews(id).then(res => {
      if (res.data.errcode === 0) {
        this.data.infoList[index].upStatus = 0
        this.data.infoList[index].upNum -= 1
        this.setData({
          infoList: this.data.infoList
        })
      }
    })
  },
  //跳转页面事件
  goto(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },
  //下拉获取新的信息列表
  getNewsListBottom(params) {
    if (this.data.isBottom) {
      wx.teamApi.homepage.newslist(params).then(res => {
        if (res.data.result.length <= 0) {
          this.data.isBottom = false
          this.data.infoParams.page -= 1
        } else {
          this.setData({
            infoList: this.data.infoList.concat(res.data.result),
          })
        }
      }).then(() => {
        this.setData({
          isLoodingHide: true
        })
      })
    } else {
      wx.showToast({
        title: '我也是有底线的！',
        icon: 'none'
      })
      this.setData({
        isLoodingHide: true
      })
    }
  },
  //上拉获取新列表
  getNewsListTop(params) {
    wx.teamApi.homepage.newslist(params).then(res => {
      let tempName=''
      for (var item of res.data.result) {
        if (item.associationName.length > 16) {
          tempName = item.associationName.substring(0, 16) + '...'
          item.associationName = tempName
        }
      }
      this.setData({
        infoList: res.data.result
      })
      console.log('122')
      console.log(this.data.infoList)
    })
  },
  //获取banner
  getBannerList() {
    wx.teamApi.homepage.banners().then(res => {
      this.setData({
        baanerList: res.data.result
      })
    })
  },
  //刷新当前页面
  refirsh() {
    this.data.infoParams.page = 1
    this.getBannerList()
    this.getNewsListTop(this.data.infoParams)
    wx.stopPullDownRefresh()
  },

  onReady: function() {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: '我新发现了一个有趣社团，我在这里等你哟',
      imgUrl: this.data.baanerList[0].img,
      path: '/pages/login/login?id=123'
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

    var that = this;
    if (app.globalData.sharePage) {
      var data = app.globalData.sharePage[0]
      wx.navigateTo({
        url: data.url + '?id=' + data.id,
      })
      app.globalData.sharePage = []
    }
    var bool = wx.getStorageSync('teamLogin') //获取登录状态
    this.setData({
      isTeamLogin: !!bool
    })

    //同步一级评论数据
    if (app.globalData.oneSynchroData) {
      this.updataInfoList(app.globalData.oneSynchroData)
    }

    this.data.isBottom = true

    //判断是否是成功发布新闻活动，显示该页面
    if (app.globalData.pushStatus) {
      this.refirsh()
      this.goTop()
      app.globalData.pushStatus = null
    }
  },
  //遍历infoList，修改点赞数，状态
  updataInfoList(oneSynchroData) {
    for (var item in this.data.infoList) {
      if (this.data.infoList[item].id == app.globalData.oneSynchroData.id) {
        console.log(this.data.infoList[item])
        this.data.infoList[item].upNum += app.globalData.oneSynchroData.dainzan
        if (this.data.infoList[item].upStatus == 1 && app.globalData.oneSynchroData.dainzan < 0) {
          this.data.infoList[item].upStatus = 0
        } else if (this.data.infoList[item].upStatus == 0 && app.globalData.oneSynchroData.dainzan > 0) {
          this.data.infoList[item].upStatus = 1
        }
        this.data.infoList[item].commentNum = app.globalData.oneSynchroData.oneCommentNum
      }
    }
    this.setData({
      infoList: this.data.infoList
    })
    app.globalData.oneSynchroData = null
  },
  //上拉触底事件
  onPullDownRefresh() {
    this.data.isfristload = true
    this.refirsh()
  },
  //下拉触底事件
  onReachBottom() {
    this.setData({
      isLoodingHide: false
    })
    this.loadmore()
  },
  //下拉加载新闻方法
  loadmore() {
    this.data.infoParams.page += 1
    this.getNewsListBottom(this.data.infoParams)
  },

  //滚动到顶部
  goTop() {
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0,
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。',
      })
    }
  }
})